function processFile(blob, fileName) { loadScriptPromise('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js').then(function() { var reader = new FileReader(); reader.onload = function(e) { var svgText = e.target.result; var img = new Image(); img.onload = function() { var w = img.naturalWidth || img.width || 800; var h = img.naturalHeight || img.height || 600; var canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; var ctx = canvas.getContext('2d'); ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, w, h); ctx.drawImage(img, 0, 0); var dataUrl = canvas.toDataURL('image/png'); var jspdfNs = window.jspdf || window.jsPDF; var JsPDFCtor = (jspdfNs && jspdfNs.jsPDF) ? jspdfNs.jsPDF : jspdfNs; var orientation = w >= h ? 'l' : 'p'; var pdf = new JsPDFCtor({ orientation: orientation, unit: 'pt', format: [w, h] }); pdf.addImage(dataUrl, 'PNG', 0, 0, w, h); var outBlob = pdf.output('blob'); add_file_output(URL.createObjectURL(outBlob), fileName.replace(/\.[^.]+$/, '.pdf')); }; img.onerror = function() { alert('Could not render this SVG file.'); }; var svgBlob = new Blob([svgText], { type: 'image/svg+xml;charset=utf-8' }); img.src = URL.createObjectURL(svgBlob); }; reader.readAsText(blob); }).catch(function() { alert('Could not load the PDF library.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }